package com.zby.business.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.zby.business.R;
import com.zby.business.net.MyAsyncTask;
import com.zby.business.util.App;
import com.zby.business.util.Util;

import java.util.ArrayList;

/**
 * Created by BY on 2016/3/6.
 */
public class ShopListViewAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private TextView shopIntroduceTextView, shopPriceTextView, shopPriceTextView1;
    private ImageView imageView, addImageView;
    private ImageLoader imageLoader;
    private ArrayList commodityPrice, commodityName, commoditypicture, commodityDetailId;
    private LruCache cache;
    private Context context;
    private String userId;

    public ShopListViewAdapter(Context context, String userId, ArrayList commodityName, ArrayList commoditypicture, ArrayList commodityPrice, ArrayList commodityDetailId) {
        this.commodityName = commodityName;
        this.commoditypicture = commoditypicture;
        this.commodityPrice = commodityPrice;
        imageLoader = ImageLoader.getInstance();
        this.commodityDetailId = commodityDetailId;
        ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(context);
        imageLoader.init(config);
        this.context = context;
        this.userId = userId;
        cache = new LruCache((int) (Runtime.getRuntime().maxMemory() / 20));
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return commodityName.size();
    }

    @Override
    public Object getItem(int position) {
        return commodityName.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.shop_listview_item, null);
        shopIntroduceTextView = (TextView) convertView.findViewById(R.id.shopIntroduceTextView);
        shopPriceTextView = (TextView) convertView.findViewById(R.id.shopPriceTextView);
        shopPriceTextView1 = (TextView) convertView.findViewById(R.id.shopPriceTextView1);
        imageView = (ImageView) convertView.findViewById(R.id.shopListViewImage);
        addImageView = (ImageView) convertView.findViewById(R.id.addToCarImageView);
        shopIntroduceTextView.setText(commodityName.get(position) + "");
        String[] price = commodityPrice.get(position).toString().split("\\.");
        final String[] url = commoditypicture.get(position).toString().split("\\;");
        shopPriceTextView.setText(price[0] + ".");
        shopPriceTextView1.setText(price[1]);
        Bitmap bitmap = (Bitmap) cache.get(url[0]);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else {
            imageLoader.displayImage(Util.BASE_URL + url[0], imageView);
            imageLoader.loadImage(Util.BASE_URL + url[0], new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {

                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {

                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                    cache.put(url[0], bitmap);
                }

                @Override
                public void onLoadingCancelled(String s, View view) {

                }
            });
        }
        addImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!userId.equals("")) {
                    MyAsyncTask asyncTask = new MyAsyncTask();
                    try {
                        String s = asyncTask.execute("phoneAddToCar?detailId=" + commodityDetailId.get(position) + "&userId=" + userId + "&price=" + commodityPrice.get(position) + "&number=1").get();
                        if (s.equals("success")) {
                            Util.toast(context, "保存成功");
                        } else {
                            Util.toast(context, "保存失败");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Util.toast(context, "保存失败");
                    }
                } else {
                    Util.toast(context, "请先登录");
                }
            }
        });
        return convertView;
    }
}
